package com.gas.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * @author guoshoudu
 * @version 1.0
 * @description
 * @date 2021/6/24 11:31 上午
 */
public class JmsConsumer_Topic_Persist {
    public static final String ACTIVEMQ_URL = "tcp://127.0.0.1:61616";
    public static final String TOPIC_NAME = "topic-persist";
    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("***我是z4消费者");
        //1创建连接工厂
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVEMQ_URL);
        // 2 通过连接工厂连接 connection  和 启动
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("z4");
        // 3 创建回话  session
        // 两个参数，第一个事务， 第二个签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 4 创建目的地 （两种 ： 队列/主题   这里用队列）
        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic,"remark.....");

        connection.start();

        Message message = topicSubscriber.receive();
        while (null!=message){
            TextMessage textMessage = (TextMessage) message;
            System.out.println("****收到的持久化topic：" +textMessage.getText());
            message= topicSubscriber.receive(1000L);
        }

        session.close();
        connection.close();
    }
}
